/*
 * @Author: 小花
 * @Date: 2022-04-07 08:51:14
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-19 12:55:14
 * @FilePath: /adminClient/src/plugins/router/router.ts
 * @Description:
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import LoginView from '@/views/Login/LoginView.vue'

import { useStore } from '@/plugins/store/store'
import nProgress from 'nprogress'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'LoginView',
    meta: {
      auth: false
    },
    component: LoginView
  },
  {
    path: '/MainView',
    name: 'MainView',
    meta: {
      auth: true
    },
    component: () => import('@/views/main/MainView.vue'),
    redirect: '/HomePage',
    children: [
      {
        path: '/HomePage',
        name: 'HomePage',
        meta: {
          auth: true,
          title: '首页',
          icon: 'fa fa-home',
          type: 1
        },
        component: () => import('@/views/main/layout/dynamicNav/HomePage.vue')
      },
      {
        path: '/EnterprisePage',
        name: 'EnterprisePage',
        meta: {
          auth: true,
          title: '企业管理',
          icon: 'fa fa-building',
          type: 1
        },
        component: () => import('@/views/main/layout/dynamicNav/EnterprisePage.vue'),
        children: [
          {
            path: '/EnterprisePage/UserManagePage',
            name: 'UserManagePage',
            meta: {
              auth: true,
              title: '用户管理',
              icon: 'fa fa-user-circle-o',
              type: 2
            },
            component: () => import('@/views/main/layout/dynamicNav/UserManagePage.vue'),
            children: [
              {
                path: '/EnterprisePage/UserManagePage/admin',
                name: 'admin',
                meta: {
                  auth: true,
                  title: '管理员',
                  icon: 'fa fa-gears',
                  type: 2
                },
                component: () => import('@/views/main/layout/dynamicNav/AdminPage.vue')
              },
              {
                path: '/EnterprisePage/UserManagePage/worker',
                name: 'worker',
                meta: {
                  auth: true,
                  title: '维修员',
                  icon: 'fa fa-wrench',
                  type: 2
                },
                component: () => import('@/views/main/layout/dynamicNav/WorkerPage.vue')
              },
              {
                path: '/EnterprisePage/UserManagePage/user',
                name: 'user',
                meta: {
                  auth: true,
                  title: '普通用户',
                  icon: 'fa fa-user-o',
                  type: 2
                },
                component: () => import('@/views/main/layout/dynamicNav/UserPage.vue')
              }
            ]
          },
          {
            path: '/EnterprisePage/AnnouncementPage',
            name: 'announcement',
            meta: {
              auth: true,
              title: '公告管理',
              icon: 'fa fa-bullhorn',
              type: 2
            },
            component: () => import('@/views/main/layout/dynamicNav/AnnouncementPage.vue')
          }
        ]
      },
      {
        path: '/FinancialManagePage',
        name: 'FinancialManagePage',
        meta: {
          auth: true,
          title: '财务管理',
          icon: 'fa fa-pie-chart',
          type: 1
        },
        component: () => import('@/views/main/layout/dynamicNav/FinancialManagePage.vue'),
        children: [
          {
            path: '/FinancialManagePage/SpendingIncomePage',
            name: 'spendingIncome',
            meta: {
              auth: true,
              title: '收支详情',
              icon: 'fa fa-yelp',
              type: 2
            },
            component: () => import('@/views/main/layout/dynamicNav/SpendingIncomePage.vue')
          }
          // {
          //   path: '/FinancialManagePage/WorkerWagePage',
          //   name: 'workerWage',
          //   meta: {
          //     auth: true,
          //     title: '员工工资',
          //     icon: 'fa fa-credit-card-alt',
          //     type: 2
          //   },
          //   component: () => import('@/views/main/layout/dynamicNav/WorkerWagePage.vue')
          // }
        ]
      },
      {
        path: '/OrdersManagePage',
        name: 'OrdersManagePage',
        meta: {
          auth: true,
          title: '订单管理',
          icon: 'fa fa-file-text-o',
          type: 1
        },
        component: () => import('@/views/main/layout/dynamicNav/OrdersManagePage.vue')
      },
      {
        path: '/PermissionPage',
        name: 'PermissionPage',
        meta: {
          auth: true,
          title: '权限管理',
          icon: 'fa fa-key',
          type: 1
        },
        component: () => import('@/views/main/layout/dynamicNav/PermissionPage.vue')
      }
    ]
  },
  {
    path: '/404',
    name: '404',
    meta: {
      auth: false
    },
    component: () => import('@/views/error/404Page.vue')
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  nProgress.start()
  if (to.meta.auth) {
    const store = useStore()
    if (store.userData && Object.keys(store.userData).length) {
      next()
    } else {
      next('/')
    }
    nProgress.done()
    return
  }
  next()
  nProgress.done()
})

export default router
